<template>
    <div class="mainDiv" style="padding: 10px; box-sizing: border-box;">
        <div class="rowHead">
            <el-select v-model="filter.cid" @change="getmsg" placeholder="请选择分类">
                <el-option label="请选择分类" value="0">
                </el-option>
                <el-option v-for="item in brandlist" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
            </el-select>
            <el-input v-model="filter.search" placeholder="请输入项目编号，名称" class="searchArea" @keyup.enter.native="getmsg">
                <el-button slot="append" icon="search" type="primary" @click="getmsg">查找</el-button>
            </el-input>
        </div>
        <el-table :data="goodslist" height="100%" @row-click="selectRow" @row-dblclick="goback" :row-class-name="tableRowClassName">
            <el-table-column prop="selected" label="选择" width="100">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.selected"></el-switch>
                </template>
            </el-table-column>
            <el-table-column prop="name" label="名称"></el-table-column>
            <el-table-column prop="number" label="编号"></el-table-column>
            <el-table-column prop="price" label="价格"></el-table-column>
            <el-table-column prop="usetime" label="用时"></el-table-column>
            <el-table-column prop="remarks" label="描述" :show-overflow-tooltip="true"></el-table-column>
        </el-table>
        <div class="rowHead">
            <el-col :span="12">
                <el-pagination @current-change="changecurrent" :current-page="filter.pages" layout="total, prev, pager, next" :total="total" :page-size="filter.limits">
                </el-pagination>
            </el-col>
            <el-col :span="12">
                <el-button style="float: right;margin-left: 5px" size="mini" type="success" icon="check" @click="goback">确定选择</el-button>
                <el-button style="float: right;" size="mini" type="danger" icon="close" @click="closeWindow">取消关闭</el-button>
            </el-col>
        </div>
    </div>
</template>
<script>
import C from '@/common/common.js'

export default {
  name: 'subjectPopup',
  data () {
    return {
      goodslist: [],
      brandlist: [],
      total: 0,
      selectrow: {},
      filter: {
        limits: 15,
        pages: 1,
        cid: '',
        search: ''
      }
    }
  },
  methods: {
    changecurrent: function (val) {
      this.filter.pages = val
      this.getmsg()
    },
    getmsg: async function () {
      let data = await C.ajax('/shop/popup/getsubjectlist', this.filter, 'post')
      if (data) {
        this.total = data.count
        this.checkselect(data.data)
      }
    },
    selectRow: function (row, event, column) {
      event.preventDefault()
      this.selectrow = row
      this.checkselect(this.goodslist)
    },
    tableRowClassName: function (row, index) {
      if (row.selected) return 'info-row'
    },
    // 计算是否选择
    checkselect: function (data) {
      data.forEach((row, index) => {
        data[index].selected = row.id == this.selectrow.id
      })
      this.goodslist = data
    },
    goback: function () {
      C.closeBrowserWindow(this.selectrow)
    },
    closeWindow: function () {
      C.closewin()
    }
  },
  mounted: async function () {
    // 获取品牌列表
    let res = await C.ajax('/shop/popup/getsubjectcategorylist', {}, 'post')
    if (res) {
      this.brandlist = res
    }
    let data = C.getDatas()
    if (data) {
      this.filter.search = data.filter.keywords
      if (data.data && data.data.data) {
        this.checkselect(data.data.data)
        this.total = data.data.count
      } else {
        this.getmsg()
      }
    } else {
      this.getmsg()
    }
  }
}
</script>